$(function(){
	$("#pms_choice_box li").mouseenter(function(){
		$(this).addClass("graybg");
	})
	
	$("#pms_choice_box li").mouseleave(function(){
		$(this).removeClass("graybg");
	})
	
	
	/*******购物车全选功能*************/
	$(".flow_fittings").slide({mainCell:".bd ul",effect:"leftLoop",vis:6,scroll:6});
	
	$(".flow_collection").slide({mainCell:".bd ul",effect:"leftLoop",vis:6,scroll:6});
	
	$("#toggle-checkboxes").click(function(){
		
		var a = $("#product-list [type='checkbox']:enabled");
		var rec_id = new Array();
		if($(this).is(":checked"))
		{
			a.each(function(i){
				$(this).prop("checked",true)
				rec_id.push($(this).val());
			})
		}
		else
		{
			a.each(function(i){
				$(this).prop("checked",false);
			})
		}
		
		$.post('flow.php?step=update_cart_selected',{
			rec_id:rec_id
			},function(result){
				if(result.error == 0)
				{
					$('#allMoney1').html('¥'+result.total_price); //更新合计	
					$('#jianmoney').html('¥'+result.total_saving); //更新节省	
					$('#amount').html('¥'+result.total_price); //更新合计
					$('#amount1').html('¥'+result.total_price); //更新合计
					$('#totalNumber').html(result.total_goods_count);//更新购物车数量
				}
				
		},'json');
	})	

	$("#product-list [type='checkbox']").click(function(){
		var a = 0;
		var rec_id = new Array();
		$("#product-list [type='checkbox']").each(function(){
			if(!$(this).is(':checked'))
			{
				a++;
			}
			else{
				rec_id.push($(this).val());
			}
			if(a > 0)
			{
				$("#toggle-checkboxes").prop("checked",false);
			}
			else
			{
				$("#toggle-checkboxes").prop("checked",true);
			}
			
			
		})
		
		$.post('flow.php?step=update_cart_selected',{
				rec_id:rec_id
				},function(result){
					if(result.error == 0)
					{
						$('#allMoney1').html('¥'+result.total_price); //更新合计	
						$('#jianmoney').html('¥'+result.total_saving); //更新节省	
						$('#amount').html('¥'+result.total_price); //更新合计
						$('#amount1').html('¥'+result.total_price); //更新合计
						$('#totalNumber').html(result.total_goods_count);//更新购物车数量
					}
					
			},'json');
	})
		
	$("#remove-batch").click(function(){
		var drop_arr = new Array();
		$("#product-list input:checked").each(function(i){
			$(this).parents(".item").remove();
			drop_arr[i] = $(this).parents(".item").find("input[name='rec_id']").val();
		})
		Ajax.call('flow.php?step=ajax_drop_goods', 'drop_arr=' + drop_arr, drop_goods_response, 'POST','JSON');
	})



	function drop_goods_response(result)
	{
		if(result.err_no == 1)
		{
			alert("程序内部错误，删除失败！");	
		}
		else
		{
			$('#amount').html('¥'+result.total_price); //更新合计
			$('#amount1').html('¥'+result.total_price); //更新合计
			$('#allMoney1').html('¥'+result.total_price); //更新合计
			$('#totalNumber').html(result.total_goods_count);//更新购物车数量
			$('#jianmoney').html('¥'+result.total_saving)
		}
	}
	
	
})


/********迷你购物车加减********/
function flowClickCartNum(a,b)
{
	var b = parseInt(b);
	var c = $("#items_"+a);
	var d = parseInt(c.val());
	if(d < 1 || !$.isNumeric(d))
	{
		alert("请输入正确的商品数量");	
		e = 1;
	}
	
	if(b == -1)		
	{
		if(d == 1)
		{
			alert("购买数量不能小于1件");	
		}
		else
		{
			e = d + b;
		}
	}
	else
	{
		e = d + b;
	}
	
	flow_change_goods_number(a,e);
}

function flowkeyUpCartNum(a,b)
{
	var c = parseInt($(a).val());
	if(c < 1 || !$.isNumeric(c))
	{
		alert("请输入正确的商品数量");	
		$(a).val(1);
		return;
	}

	flow_change_goods_number(b,c);

}

function flow_change_goods_number(rec_id, goods_number)
{     
	Ajax.call('flow.php?step=ajax_update_cart', 'rec_id=' + rec_id +'&goods_number=' + goods_number, flow_change_goods_number_response, 'POST','JSON');
}
function flow_change_goods_number_response(result)
{              

	if (result.error == 0)
	{
		
		
		
	}
	else if (result.message != '')
	{
		alert(result.message);                
	}
	var rec_id = result.rec_id;
	$('#items_' +rec_id).val(result.goods_number);//更新数量	
	$('#total_items_' +rec_id).html('<span>'+result.goods_subtotal+'</span>');//更新小计
	$('#allMoney1').html('¥'+result.total_price); //更新合计	
	$('#money0').html('¥'+result.total_price); //更新合计	
	$('#jianmoney').html('¥'+result.total_saving); //更新节省	
	$('#amount').html('¥'+result.total_price); //更新合计
	$('#amount1').html('¥'+result.total_price); //更新合计
	$('#totalNumber').html(result.total_goods_count);//更新购物车数量
	$('.bag_total_info_box').html(result.total_info); 
}

